/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view.component;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import org.springframework.web.util.TagUtils;

/**
 *
 * @author Walter Lütgenau
 */
public class StreamComponent extends UIComponentBase {

    public static final String PAR_CONTENT = "content";

    @Override
    public String getFamily() {
        return "htmlNodeFamily";
    }

    public void encodeBegin(FacesContext context) throws IOException {
        if (!isRendered())
            return;

        ResponseWriter writer = context.getResponseWriter();
        InputStream content = (InputStream) getAttributes().get(PAR_CONTENT);

        InputStreamReader reader = null;

        try {
            reader = new InputStreamReader((InputStream) content);
            char[] buf = new char[4096];
            int count = 0;

            while ((count = reader.read(buf)) > 0) {
                writer.write(buf, 0, count);
            }
        } finally {
            if (reader != null)
                reader.close();
        }

    }
}
